package com.uphone.magicleafteacher.pro.activity.welcome;

import android.arch.lifecycle.LifecycleOwner;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.google.gson.Gson;
import com.radish.baselibrary.utils.EncryptUtils;
import com.radish.baselibrary.utils.LogUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.base.mvp.BasePAV;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.LoginBean;

import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by hzy on 2019/1/18
 * WelcomePresenter
 *
 * @author hzy
 */
public class WelcomePresenter extends BasePAV<WelcomeContract.View> implements WelcomeContract.Presenter {
    private WeakReference<WelcomeContract.View> mReference;
    private WelcomeActivity activity;

    @Inject
    WelcomePresenter() {

    }


    @Override
    public void attachView(WelcomeContract.View view) {
        super.attachView(view);
        mReference = new WeakReference<WelcomeContract.View>(view);
    }

    public void attachView(WelcomeActivity activity) {
        this.activity = activity;
    }

    @Override
    public void detachView() {
        if (mReference != null) {
            mReference.clear();
            mReference = null;
        }
        super.detachView();
    }

    @Override
    public void postLogin(String username, String password) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("loginName", username);
        map.put("password", password);
        MyApp.apiService(ApiService.class)
                .loginService(map)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    activity.showLoading();
                })
                .doFinally(() -> {
                    activity.closeLoading();
                })
                .subscribe(bean -> {
                    if (bean != null) {
                        try {
                            String jsonStr = bean.string();
                            Gson gson = GsonUtils.getGson();
                            LoginBean loginBean = gson.fromJson(jsonStr, LoginBean.class);
                            if (loginBean == null)
                                return;
                            if (loginBean.getCode() == 0) {
                                activity.updateView(loginBean);
                            } else if (!TextUtils.isEmpty(loginBean.getMsg())) {
                                ToastUtils.showShort(loginBean.getMsg());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, throwable -> {
                    activity.onFail();

                });
    }
}
